package io.renren.modules.sys.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.constans.GoodTypeCodeConstans;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysCodeEntity;
import io.renren.modules.sys.entity.SysCodetypeEntity;
import io.renren.modules.sys.service.SysCodeService;
import io.renren.modules.sys.service.SysCodetypeService;




/**
 * @author chaolemen
 * @email 2698481037@qq.com
 * @date 2017-12-24 15:54:03
 */
@RestController
@RequestMapping("/sys/syscode")
public class SysCodeController {
	
	@Autowired
	private SysCodeService sysCodeService;
	@Autowired
	private SysCodetypeService sysCodetypeService;
	
	/**
	 * 列表 2017年12月26日
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:syscode:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SysCodeEntity> sysCodeList = sysCodeService.queryList(query);
		int total = sysCodeService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(sysCodeList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 淘宝商品分类 taobao_good_cat
	 * @param params
	 * @return
	 */
	@RequestMapping("/listgoodcat")
	@RequiresPermissions("sys:syscode:list")
	public R listgoodcat(){
		List<SysCodeEntity> sysCodeList = sysCodeService.queryListByTypeCode(GoodTypeCodeConstans.CAT_TAOBAO_GOOD);
		return R.ok().put("sysCodeList", sysCodeList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:syscode:info")
	public R info(@PathVariable("id") Long id){
		SysCodeEntity sysCode = sysCodeService.queryObject(id);
		
		return R.ok().put("sysCode", sysCode);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:syscode:save")
	public R save(@RequestBody SysCodeEntity sysCode){
		SysCodetypeEntity sysCodetypeEntity = sysCodetypeService.queryByTypeCode(sysCode.getTypeCode());
		if(sysCodetypeEntity != null){
			sysCode.setTypeName(sysCodetypeEntity.getTypeName());
		}
		sysCodeService.save(sysCode);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:syscode:update")
	public R update(@RequestBody SysCodeEntity sysCode){
		SysCodetypeEntity sysCodetypeEntity = sysCodetypeService.queryByTypeCode(sysCode.getTypeCode());
		if(sysCodetypeEntity != null){
			sysCode.setTypeName(sysCodetypeEntity.getTypeName());
		}
		sysCodeService.update(sysCode);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:syscode:delete")
	public R delete(@RequestBody Long[] ids){
		sysCodeService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
